import request from '@/utils/request'
import * as Api from '@/api' // 所有请求方法

// index store
export const state = () => ({
  isSpider: false,
  browser: process.browser,
  city: {}, // 当前城市
  cityList: [], // 城市列表
  reserveList: [] // 预约列表
})

export const mutations = {
  // 刷新当前store
  SET_INDEX(state, data) {
    Object.entries(data).forEach(([key, value]) => {
      state[key] = value
    })
  },
  // 切换城市
  CHANGE_CITY(state, city) {
    state.city = city
  }
}

export const getters = {
  shop_id(state) {
    const { city = {} } = state.city
    return city.shop_id
  }
}

export const actions = {
  // 请求方法
  fetch({ state }, options) {
    const { headers = {} } = options
    const { token } = state.user.user || {}
    if (token) {
      headers['Authorization'] = `Bearer ${token}`
    }
    const { region_code } = state.city
    headers['X-Halo-Region'] = region_code || 310100
    options.headers = headers

    return request(options)
  },
  ...Api
}
